#pragma once
#include "StdAfx.h"
#include "EntityControl.h"
#include "Controller.h"
#include "DamageAction.h"
#include "PSysAction.h"
#include "DeleteAction.h"
#include "UltimateSoundSubsystem.h"
#include "SetParameterAction.h"
#include "EntityParameter.h"
#include "IntEntityParameter.h"
#include "UltimatePhysicsContactListener.h"

//class EntityControl;

class ContactReport : public UltimatePhysicsContactListener
{	

	Ogre::SceneManager * mSceneMgr;
	Ogre::SceneManager * mControlSceneMgr;

	std::deque<Ogre::ParticleSystem*> *pSystems;
	UltimateSoundSubsystem *audio;
	


	virtual void onContactNotify(UltimatePhysicsActor * actor1,	UltimatePhysicsActor * actor2)
	{		
		Ogre::String* Actor1Name = static_cast<Ogre::String*>( actor1->getUserData());	
		Ogre::String* Actor2Name = static_cast<Ogre::String*>( actor2->getUserData());
		
		EntityControl* Actor1UserData;
		EntityControl* Actor2UserData;

		try{
		
			Actor1UserData = (EntityControl*) mControlSceneMgr->getManualObject(*Actor1Name);
			Actor2UserData = (EntityControl*) mControlSceneMgr->getManualObject(*Actor2Name);
		}catch(Ogre::Exception e)
		{
			return;
		}



		/*if((!Actor1UserData->isActorInited()) || (!Actor2UserData->isActorInited()))
		{
			return;
		}*/


		//Penguin vs meatball
		if(actor1->getGroup()==2 && actor2->getGroup()==3)
		{
			Trigger *damage = new Trigger(); 
			damage->addAction(new DamageAction(10));
			damage->addAction(new PSysAction(mSceneMgr,"paff",pSystems));
			Actor1UserData->AddTrigger(damage);

			Trigger *kill = new Trigger(); 

			//audio->playAudioFromFile("media/audio/sound/Slap.wav",false,1.0,Actor2UserData->getParentNode()->getPosition());

		
			kill->addAction(new DeleteAction());
			Actor2UserData->AddTrigger(kill);

			//pair.actors[1]->setGroup(0);

			return;
		}

		//Penguin vs meatball
		if(actor1->getGroup()==3 && actor2->getGroup()==2)
		{
			Trigger *damage = new Trigger(); 
			damage->addAction(new DamageAction(10));
			damage->addAction(new PSysAction(mSceneMgr,"paff",pSystems));
			Actor2UserData->AddTrigger(damage);

			Trigger *kill = new Trigger(); 

			//audio->playAudioFromFile("media/audio/sound/Slap.wav",false,1.0,Actor2UserData->getParentNode()->getPosition());

		
			kill->addAction(new DeleteAction());
			Actor1UserData->AddTrigger(kill);

			//pair.actors[0]->setGroup(0);

			return;
		}

		////Sword vs meatball
		//if(actor1->getGroup()==1 && actor2->getGroup()==3)
		//{
		//	Trigger *score = new Trigger();
		//	int currScore =((IntEntityParameter*)Actor1UserData->getParameter(SCORE_PARAMETER))->getIntValue();
		//	currScore++;
		//	score->addAction(new SetParameterAction(new IntEntityParameter(SCORE_PARAMETER,currScore)));
		//	Actor1UserData->AddTrigger(score);


		//	Trigger *kill = new Trigger(); 
		//
		//	kill->addAction(new PSysAction(mSceneMgr,"explode2",pSystems));
		//	kill->addAction(new DeleteAction());
		//	Actor2UserData->AddTrigger(kill);

		//	audio->playAudioFromFile("media/audio/sound/SlimeSplash.wav",false,1.0,Actor2UserData->getParentNode()->getPosition());



		//	return;
		//}

		////Sword vs meatball
		//if(actor1->getGroup()==3 && actor2->getGroup()==1)
		//{
		//	Trigger *kill = new Trigger(); 
		//
		//	kill->addAction(new PSysAction(mSceneMgr,"explode2",pSystems));
		//	kill->addAction(new DeleteAction());
		//	Actor1UserData->AddTrigger(kill);

		//	audio->playAudioFromFile("media/audio/sound/SlimeSplash.wav",false,1.0,Actor2UserData->getParentNode()->getPosition());


		//	Trigger *score = new Trigger();
		//	int currScore =((IntEntityParameter*)Actor2UserData->getParameter(SCORE_PARAMETER))->getIntValue();
		//	currScore++;
		//	score->addAction(new SetParameterAction(new IntEntityParameter(SCORE_PARAMETER,currScore)));
		//	Actor2UserData->AddTrigger(score);


		//	return;

		//}

		//Ship vs asteroid
		if(actor1->getGroup()==6 && actor2->getGroup()==1)
		{
			Trigger *damage = new Trigger(); 
			damage->addAction(new DamageAction(2000));
			damage->addAction(new PSysAction(mSceneMgr,"fire_explode",pSystems));
			Actor2UserData->AddTrigger(damage);

			//audio->playAudioFromFile("media/audio/sound/CarExplosion.wav",false,1.0,Actor2UserData->getParentNode()->getPosition());

			/*Trigger *kill = new Trigger(); 
		
			kill->addAction(new DeleteAction());
			Actor1UserData->AddTrigger(kill);*/

			//pair.actors[0]->setGroup(0);

			return;
		}

		//Ship vs asteroid
		if(actor1->getGroup()==1 && actor2->getGroup()==6)
		{
			Trigger *damage = new Trigger(); 
			damage->addAction(new DamageAction(2000));
			damage->addAction(new PSysAction(mSceneMgr,"fire_explode",pSystems));
			Actor1UserData->AddTrigger(damage);

			//audio->playAudioFromFile("media/audio/sound/CarExplosion.wav",false,1.0,Actor2UserData->getParentNode()->getPosition());

			/*Trigger *kill = new Trigger(); 
		
			kill->addAction(new DeleteAction());
			Actor2UserData->AddTrigger(kill);*/

			//pair.actors[1]->setGroup(0);

			return;
		}


		//Meatball vs asteroid
		if(actor1->getGroup()==6 && actor2->getGroup()==3)
		{
			/*Trigger *deleteTrigger = new Trigger(); 
			deleteTrigger->addAction(new DeleteAction());
			deleteTrigger->addAction(new PSysAction(mSceneMgr,"fire_explode",pSystems));
			Actor1UserData->AddTrigger(deleteTrigger);*/

			//audio->playAudioFromFile("media/audio/sound/CarExplosion.wav",false,1.0,Actor2UserData->getParentNode()->getPosition());

			Trigger *deleteTrigger2 = new Trigger(); 
			deleteTrigger2->addAction(new DeleteAction());
			deleteTrigger2->addAction(new PSysAction(mSceneMgr,"fire_explode",pSystems));
			Actor2UserData->AddTrigger(deleteTrigger2);

			return;
		}

		//Meatball vs asteroid
		if(actor1->getGroup()==3 && actor2->getGroup()==6)
		{
			Trigger *deleteTrigger = new Trigger(); 
			deleteTrigger->addAction(new DeleteAction());
			deleteTrigger->addAction(new PSysAction(mSceneMgr,"fire_explode",pSystems));
			Actor1UserData->AddTrigger(deleteTrigger);

			//audio->playAudioFromFile("media/audio/sound/CarExplosion.wav",false,1.0,Actor2UserData->getParentNode()->getPosition());

			/*Trigger *deleteTrigger2 = new Trigger(); 
			deleteTrigger2->addAction(new DeleteAction());
			deleteTrigger2->addAction(new PSysAction(mSceneMgr,"fire_explode",pSystems));
			Actor2UserData->AddTrigger(deleteTrigger2);*/

			return;
		}


		//Ship vs star
		if(actor1->getGroup()==1 && actor2->getGroup()==9)
		{
			

			IntEntityParameter param(SCORE_PARAMETER,((IntEntityParameter*)Actor1UserData->getParameter(SCORE_PARAMETER))->getIntValue()+100);
			Actor1UserData->setParameter(SCORE_PARAMETER,&param);

			Trigger *deleteTrigger2 = new Trigger(); 
			deleteTrigger2->addAction(new DeleteAction());
			deleteTrigger2->addAction(new PSysAction(mSceneMgr,"explode2",pSystems));
			Actor2UserData->AddTrigger(deleteTrigger2);


			return;
		}

		//Star vs Ship
		if(actor1->getGroup()==9 && actor2->getGroup()==1)
		{
			Trigger *deleteTrigger = new Trigger(); 
			deleteTrigger->addAction(new DeleteAction());
			deleteTrigger->addAction(new PSysAction(mSceneMgr,"explode2",pSystems));
			Actor1UserData->AddTrigger(deleteTrigger);

			IntEntityParameter param(SCORE_PARAMETER,((IntEntityParameter*)Actor2UserData->getParameter(SCORE_PARAMETER))->getIntValue()+100);
			Actor2UserData->setParameter(SCORE_PARAMETER,&param);

			return;
		}


	// if actor1userdata.entitytype == car || actor2userdata.entitytype == car		
	//		play car smash sound, spawn particle emitter etc	
	}


public:
	void setSceneManager(Ogre::SceneManager *scm)
	{
		mSceneMgr = scm;
	}

	void setControlSceneManager(Ogre::SceneManager *scm)
	{
		mControlSceneMgr = scm;
	}

	void setParticleSystems(std::deque<Ogre::ParticleSystem*> *pSs)
	{
		pSystems = pSs;
	}

	void setGameAudio(UltimateSoundSubsystem *aud)
	{
		audio = aud;
	}

};